package edu.langnan.xiaoli.service.impl;

import edu.langnan.xiaoli.core.domain.PictureDO;
import edu.langnan.xiaoli.exception.file.FileEmptyException;
import edu.langnan.xiaoli.mapper.PictureMapper;
import edu.langnan.xiaoli.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author cql
 */
@Service
public class PictureServiceImpl implements PictureService {

    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public PictureDO insertPicture(String path, String url) {
        if ("".equals(path) || "".equals(url)) {
            throw new FileEmptyException();
        }
        PictureDO pictureDO = new PictureDO();
        pictureDO.setPath(path);
        pictureDO.setUrl(url);

        pictureMapper.insert(pictureDO);

        return pictureDO;
    }

    @Override
    public void deleteImage(PictureDO pictureDO) {
        if (pictureDO != null && pictureDO.getId() != null && pictureDO.getId() != 0) {
            pictureMapper.deleteById(pictureDO.getId());
        }
    }
}
